package com.wms.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.wms.bean.PageBean;
import com.wms.code.CodeFactory;
import com.wms.dto.*;
import com.wms.entity.BReplenishmentDerail;
import com.wms.entity.BReviewDetails;
import com.wms.entity.ReplenishmentOrder;

import com.wms.exception.AutditException;
import com.wms.mapper.BReplenishmentDerailMapper;
import com.wms.service.BReplenishmentDerailService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class BReplenishmentDerailServiceImpl implements BReplenishmentDerailService {
    @Autowired
    BReplenishmentDerailMapper bReplenishmentDerailMapper;

    //补货单的编码前缀
    String OrderPrefix = "BH";
    //补货明细编码前缀
    String Prefix = "BH";

    @Override
    public PageBean<List<BReplenishmentDerail>> queryOrderDerail(BReplenishmentsDerailDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<BReplenishmentDerail> list =bReplenishmentDerailMapper.queryOrderDerail(dto);
        PageInfo<BReplenishmentDerail> pageInfo = new PageInfo<>(list);
        return new PageBean<List<BReplenishmentDerail>>(dto.getPage(), dto.getPageSize(), pageInfo.getTotal(),pageInfo.getList());
    }

    @Override
    public PageBean<List<ReplenishmentOrder>> queryOrders(BReplenishmentDerailDTO dto) {
        PageHelper.startPage(dto.getPage(), dto.getPageSize());
        List<ReplenishmentOrder> list  = bReplenishmentDerailMapper.queryOrders(dto);
        PageInfo<ReplenishmentOrder> pageInfo = new PageInfo<>(list);
        return new PageBean<List<ReplenishmentOrder>>(dto.getPage(), dto.getPageSize(), pageInfo.getTotal(),pageInfo.getList());
    }


    @Override
    public int addOrder(ReplenishmentOrder replenishmentOrder) {
        //新增前查询最大编码
       String  MaxCode = bReplenishmentDerailMapper.selectMaxCode();
       //设置最大编码
       replenishmentOrder.setCode(CodeFactory.createArrivalCode(MaxCode,OrderPrefix));
       return bReplenishmentDerailMapper.insertOrder(replenishmentOrder);
    }


    @Override
    public List<BReviewDetails> selectStores(Integer detailId) {
        return bReplenishmentDerailMapper.selectStores(detailId);
    }


    /**
     * 审核通过门店信息修改
     * @param
     * @return
     */
    @Override
    @Transactional
    public int updateAuditNum(StroresAuditDerailDTO stroresAuditDerailDTO) {
        //取出数据进项操作
        int result = 0;
        //批量审核操作
        //对审核的数据进行修改
        int result1 = bReplenishmentDerailMapper.updateAuditDerail(stroresAuditDerailDTO.getDetailId(),stroresAuditDerailDTO.getAuditStatus());
        if(result1==1){
            List<StoresAuditDTO> list = stroresAuditDerailDTO.getStoresAuditDTOS();
            for(int i = 0; i<list.size();i++){
                StoresAuditDTO storesAuditDTO = list.get(i);
                bReplenishmentDerailMapper.updateAuditNum(storesAuditDTO.getId(),storesAuditDTO.getAuditNum());
                result = 1;
            }
        }else {
            Map map = new HashMap();
            map.put("门店审核内容","审核失败");
            throw new AutditException(new HashMap(map));
        }
        return result;
    }


    @Override
    @Transactional
    public int insertProductAndStores(SaveAuditProductDTO saveAuditProductDTO) {
        int j=0;
        //提取需要添加商品和门店的id集合
        List<SaveProductAndStoresDTO> saveProductAndStoresDTOS = saveAuditProductDTO.getList();
        //批量进行插入
        for(int i = 0;i<saveProductAndStoresDTOS.size();i++){
            SaveProductAndStoresDTO saveProductAndStoresDTO = saveProductAndStoresDTOS.get(i);
           int n= bReplenishmentDerailMapper.insertProductAndStores(saveProductAndStoresDTO.getProductId(),saveProductAndStoresDTO.getStoresId());
           if(n==1){
               j++;
           }
        }
        return j==saveProductAndStoresDTOS.size()?1:0;
    }
}
